﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Diagnostics;
using Owin;
using Newtonsoft.Json;

namespace DiscreteMath.WebApp
{
	public class Startup
	{
		public void Configuration(IAppBuilder app)
		{
			// Any connection or hub wire up and configuration should go here
			var config = new HubConfiguration();
			config.EnableJSONP = true;
			config.EnableDetailedErrors = true;
			app.MapSignalR(config);
		}
	}   

    public class Dispatcher : Hub
    {
        public NondeterministicAutomaton Determine(string automatonJson)
        {
			NondeterministicAutomaton automaton = JsonConvert.DeserializeObject<NondeterministicAutomaton>(automatonJson);
			return automaton.Determine();
        }

		public DeterministicAutomaton Minimize(string automatonJson)
		{
			NondeterministicAutomaton automaton = JsonConvert.DeserializeObject<NondeterministicAutomaton>(automatonJson);
			return automaton.Determine().Minimize();
		}

		public NondeterministicAutomaton KleneeStar(string automatonJson)
		{
			NondeterministicAutomaton automaton = JsonConvert.DeserializeObject<NondeterministicAutomaton>(automatonJson);
			return automaton.KleneeStar();
		}

		public DeterministicAutomaton Negate(string automatonJson)
		{
			NondeterministicAutomaton automaton = JsonConvert.DeserializeObject<NondeterministicAutomaton>(automatonJson);
			return automaton.Determine().Negate();
		}

		public bool RecognizeWord(string automatonJson, string word)
		{
			NondeterministicAutomaton automaton = JsonConvert.DeserializeObject<NondeterministicAutomaton>(automatonJson);
			return automaton.ReadWord(word);
		}

		public NondeterministicAutomaton Concatenate(string firstAutomatonJson, string secondAutomatonJson)
		{
			NondeterministicAutomaton first = JsonConvert.DeserializeObject<NondeterministicAutomaton>(firstAutomatonJson),
				second = JsonConvert.DeserializeObject<NondeterministicAutomaton>(secondAutomatonJson);

			return first.Concatenate(second);
		}

		public NondeterministicAutomaton Union(string firstAutomatonJson, string secondAutomatonJson)
		{
			NondeterministicAutomaton first = JsonConvert.DeserializeObject<NondeterministicAutomaton>(firstAutomatonJson),
				second = JsonConvert.DeserializeObject<NondeterministicAutomaton>(secondAutomatonJson);

			return first.Union(second);
		}

		public DeterministicAutomaton Intersection(string firstAutomatonJson, string secondAutomatonJson)
		{
			NondeterministicAutomaton first = JsonConvert.DeserializeObject<NondeterministicAutomaton>(firstAutomatonJson),
				second = JsonConvert.DeserializeObject<NondeterministicAutomaton>(secondAutomatonJson);

			return first.Determine().Intersection(second.Determine());
		}


    }
}